/*  Copyright 2011 Jason Jones
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.jasonwjones.hyperion.jessub.formattingvalue.date;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;

/**
 * Represents a set of date adjustments. Essentially this is a representation 
 * of the contents of the NOW() argument.
 * @author jasonwjones
 *
 */
public class DateAdjustmentSet {
	
	/** all of the individual date adjustments will be stored here */
	protected List<DateAdjustment> dateAdjustments = new ArrayList<DateAdjustment>();
	
	/**
	 * Pass in the array of values from the tag
	 * @param dateAdjustments
	 */
	public DateAdjustmentSet(String[] dateAdjustments) {
		for (String dateAdjustment : dateAdjustments) {
			// guard here against blanks -- could do this in .createFrom but we
			// don't want to just end up with a useless adjustment here
			if (!dateAdjustment.trim().isEmpty()) {
				this.addDateAdjustment(DateAdjustment.createFrom(dateAdjustment));
			}
		}
	}
	
	/**
	 * Add an adjustment to the list of adjustments to make to the date
	 * @param dateAdjustment
	 */
	public void addDateAdjustment(DateAdjustment dateAdjustment) {
		dateAdjustments.add(dateAdjustment);
	}
	
	/**
	 * Applies all of the adjustments to the DateTime by running through
	 * the list and applying them one at a time.
	 * @param dateTime the DateTime value to adjust
	 * @return a new DateTime object that has had its date value adjusted
	 * by all of the adjustments in this class
	 */
	public DateTime applyTo(DateTime dateTime) {
		DateTime adjustedDateTime = dateTime;
		
		for (DateAdjustment dateAdjustment : dateAdjustments) {
			adjustedDateTime = dateAdjustment.applyTo(adjustedDateTime);
		}
		
		return adjustedDateTime;
	}

	/**
	 * Simple string 
	 */
	@Override
	public String toString() {
		return dateAdjustments.toString();
	}
	
}
